package com.itheima.d4_algorithm;

/**
 * 目标：掌握二分查找算法。
 *
 * binarySearch
 */
public class Test3 {
    public static void main(String[] args) {
        int[] ints=new int[]{1,2,3,4,5,6,7,8};
        System.out.println(get(ints,4));
    }
    public static int get(int[] ints,int n){
        int mid;
        int right=0;
        int left=ints.length-1;
        while(right<=left){
            mid=(left+right)/2;
            if(ints[mid]<n){
                right=mid+1;
            }else if(ints[mid]>n){
                left=mid-1;
            }else{
                return mid;
            }
        }
        return -1;
    }
}
